#include "StdAfx.h"
#include "../Utilities/utilities.h"
#include "glTexture.h"

glTexture::glTexture(void)
{
}

glTexture::~glTexture(void)
{
}

GLuint glTexture::LoadTexture()
{
	int	 width = 512 ,height = 512 , buffer=0;
	GLuint textureId = 0;
	
	char * buf = LoadTGA(path_Texture,&width,&height,&buffer);
	glGenTextures(1,&textureId);
	glBindTexture ( GL_TEXTURE_2D, textureId );
   
	glTexImage2D ( GL_TEXTURE_2D, 0,(buffer==24)?GL_RGB:GL_RGBA, width, height, 0, (buffer==24)?GL_RGB:GL_RGBA, GL_UNSIGNED_BYTE, buf );

	glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	if(tiling==REPEAT){
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	}
	
	if(tiling==CLAMP){
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	}

	delete []buf;
	id_Texture=textureId;
	return textureId;
}

GLuint glTexture::LoadEnvironment()
{
	int	 width = 512 ,height = 512 , buffer=0;
	GLuint textureId = 0;
	char **buf;
	buf=new char*[6];
	for(int i=0;i<6;i++)
	{
		buf[i] = LoadTGA(path_CubeTexture[i],&width,&height,&buffer);
	}
	glGenTextures(1,&textureId);
	glBindTexture ( GL_TEXTURE_CUBE_MAP, textureId );

	for (int i=0; i<6; i++) 
	{ 
		glTexImage2D (  
					   GL_TEXTURE_CUBE_MAP_POSITIVE_X+i, 
					   0, 
					   GL_RGB, 
					   width, 
					   height, 
					   0, 
					   GL_RGB, 
					   GL_UNSIGNED_BYTE, 
					   buf[i] ); 
	}

	glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
	glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
	for(int i=0;i<6;i++){
		delete []buf[i];
	}
	delete buf;
	id_TextureSky=textureId;
	return textureId;
}

void glTexture::CleanUp(){
	delete path_Texture;
}
